# -*- coding: UTF-8 -*-
# 可编程直流电源

import serial.tools.list_ports
import binascii

class Digitalpower(object):
    # 打开串口
    def __init__(self):
        plist = list(serial.tools.list_ports.comports())
        if len(plist) <= 0:
            print("can not find port !")
        else:
            plist_num = list(plist[1])
            serialport = plist_num[0]
            print (serialport)
            self.serialFd = serial.Serial(port=serialport, baudrate=9600, timeout=2, bytesize=8, parity='N',
                                     stopbits=1)

    # 设置数字电源电压
    def Set_Digitalpower_volt(self,response):
        # 初始化环境
        #cmd = "[:]VOLT %f" % volt
        serialFd = self.serialFd
        #serialFd.write(cmd)
        #cmd = "[:]MEAS:VOLT?"
        cmd =list()
        hLen = len(response)
        for i in range(hLen):
            hvol = ord(response[i])
            hhex = '%02x' % hvol
            string='0x'+hhex
            cmd.append(string)
        print (cmd)
        cmd =[0x2A, 0x49, 0x44, 0x4E, 0x3F, 0x0A]
        #cmd =[0x5b,0x3a,0x5d,0x4d,0x45,0x41,0x53,0x3a,0x56,0x4f,0x4c,0x54,0x3f]

        serialFd.write(cmd)
        response = serialFd.readall()
        print (response)


d=Digitalpower()
d.Set_Digitalpower_volt("[:]MEAS:VOLT?")
